<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
class Server extends Controller
{
    public function index()
    {
        if(!empty($_GET['username'])){
            $userid = Db::name('userinfo')
            ->where("user_name",$_GET['username'])
            ->find();
            $num = count(Db::name('reserveinfo')
            ->where('user_id',$userid['id'])
            ->select());
            $result =Db::table('reserveinfo')
            ->alias('a')
            ->where('user_id',$userid['id'])
            ->join('userinfo b','a.user_id = b.id')
            ->paginate(1);
        }else{
            $num = count(Db::name('reserveinfo')
            ->select());
            $result =Db::table('reserveinfo')
            ->alias('a')
            ->join('userinfo b','a.user_id = b.id')
            ->paginate(5);
        }
        
        $this->assign("result",$result);
        $this->assign("num",$num);
        return $this->fetch('server/serverlist');
    }
    public function upserverstate(){
        if(!empty($_GET['id'])){
            $res=Db::name('reserveinfo')
            ->where("id",$_GET['id'])
            ->find();
        }
        $this->assign("res",$res);
        return $this->fetch('server/upserver');
    }
    public function upserver(){
        if(!empty($_POST['state'])){
            $res=Db::name('reserveinfo')
            ->where("id",$_POST['id'])
            ->update(['reserve_sta'=>$_POST['state']]);
            if($res>0){
                return 'ok';
            }
        }
    }
}
